<!-- The code here is taken from WPT BiDi Tests repo https://github.com/web-platform-tests/wpt/blob/master/webdriver/tests/support/html/test_actions.html-->
<!-- Giving thanks to the folks contributing to https://github.com/web-platform-tests/wpt/tree/master/webdriver/tests/bidi Thank you! -->
<!doctype html>
<meta charset=utf-8>
<html>
<head>

    <title>Test Actions</title>
    <p> This HTML page is taken from  <a href="https://github.com/web-platform-tests/wpt/blob/master/webdriver/tests/support/html/test_actions.html">WPT BiDi Tests.</a> Thank you so much folks at <a href="https://github.com/web-platform-tests/wpt/"> web-platform-tests </a> for your contribution!</p>

    <style>
      div { padding: 0; margin: 0; }
      #trackPointer { position: fixed; }
      #resultContainer { width: 600px; height: 60px; }
      .area { width: 100px; height: 50px; background-color: #ccc; }
      .block { width: 5px; height: 5px; border: solid 1px red; }
      .box { display: flex;}
      #dragArea { position: relative; }
      #dragTarget { position: absolute; top:22px; left:47px;}
    </style>
    <script>
        "use strict";
        var els = {};
        var allEvents = { events: [] };
        function displayMessage(message) {
            document.getElementById("events").innerHTML = "<p>" + message + "</p>";
        }

        function appendMessage(message) {
            document.getElementById("events").innerHTML += "<p>" + message + "</p>";
        }

        /**
         * Escape |key| if it's in a surrogate-half character range.
         *
         * Example: given "\ud83d" return "U+d83d".
         *
         * Otherwise JSON.stringify will convert it to U+FFFD (REPLACEMENT CHARACTER)
         * when returning a value from executeScript, for example.
         */
        function escapeSurrogateHalf(key) {
          if (typeof key !== "undefined" && key.length === 1) {
            var charCode = key.charCodeAt(0);
            var highSurrogate = charCode >= 0xD800 && charCode <= 0xDBFF;
            var surrogate = highSurrogate || (charCode >= 0xDC00 && charCode <= 0xDFFF);
            if (surrogate) {
              key = "U+" + charCode.toString(16);
            }
          }
          return key;
        }

        function recordKeyboardEvent(event) {
          var key = escapeSurrogateHalf(event.key);
          allEvents.events.push({
            "code": event.code,
            "key": key,
            "which": event.which,
            "location": event.location,
            "ctrl": event.ctrlKey,
            "meta": event.metaKey,
            "shift": event.shiftKey,
            "repeat": event.repeat,
            "type": event.type
          });
          appendMessage(event.type + " " +
              "code: " + event.code + ", " +
              "key: " + key + ", " +
              "which: " + event.which + ", " +
              "keyCode: " + event.keyCode);
        }


        function resetEvents() {
          allEvents.events.length = 0;
          displayMessage("");
        }

        function drop(moveHandler) {
          return function (event) {
            els.dragArea.removeEventListener("mousemove", moveHandler);
            els.dragTarget.style.backgroundColor = "yellow";
            els.dragTarget.addEventListener("mousedown", grab);
            recordPointerEvent(event);
          };
        }


        document.addEventListener("DOMContentLoaded", function() {
          var keyReporter = document.getElementById("keys");
          keyReporter.addEventListener("keyup", recordKeyboardEvent);
          keyReporter.addEventListener("keypress", recordKeyboardEvent);
          keyReporter.addEventListener("keydown", recordKeyboardEvent);
        });
    </script>
</head>
<body>
  <div>
    <h2>KeyReporter</h2>
    <input type="text" id="keys" size="80">
  </div>
  <div id="resultContainer">
    <h2>Events</h2>
    <div id="events"></div>
  </div>
</body>
</html>